package dao;

import pojo.Menu;
import pojo.Role;
import utils.DbUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends DbUtils implements RoleDao {
    @Override
    public List<Role> selectALl(Integer pageIndex,Integer pageSize) {
        String sql = "select * from role limit ?,?";
        List<Role> roleList = new ArrayList<>();
        List list = new ArrayList();
        list.add((pageIndex-1)*pageSize);
        list.add(pageSize);
        ResultSet query = query(sql, list);
        try {
            while (query.next()){
                Role role = new Role();
                role.setRoleId(query.getInt("roleid"));
                role.setRoleName(query.getString("rolename"));
                role.setRoleState(query.getInt("rolestate"));
                roleList.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return roleList;
    }

    @Override
    public int total() {
        String sql = "select count(*) from role";
        ResultSet query = query(sql, null);
        int total = 0;
        try {
            while (query.next()){
                total = query.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int addRole(Role role) {
        String sql = "insert into role values(null,?,?)";
        int key=0;//新增数据的id
        List list = new ArrayList();
        list.add(role.getRoleName());
        list.add(role.getRoleState());
        update(sql, list);//受影响行数
        try {
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            if (generatedKeys.next()){
                key = generatedKeys.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        closeAll();
        return key;
    }

    @Override
    public Role findByid(int roleid) {
        Role role = new Role();
        List<Menu> menuList = new ArrayList<>();
        String sql = "select * from role r,middle m,menu e where r.roleid = m.roleid and m.menuid = e.menuid and r.roleid=?";
        List list =new ArrayList();
        list.add(roleid);
        ResultSet query = query(sql, list);
        try {
            while (query.next()){
                role.setRoleId(query.getInt("roleid"));
                role.setRoleName(query.getString("rolename"));
                role.setRoleState(query.getInt("rolestate"));
                Menu menu = new Menu();
                menu.setMenuId(query.getInt("menuid"));
                menu.setMenuName(query.getString("menuname"));
                menu.setUrl(query.getString("url"));
                menu.setState(query.getInt("state"));
                menu.setUpmenuId(query.getInt("upmenuid"));
                menuList.add(menu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        role.setMenuList(menuList);
        return role;
    }
    @Override
    public int deletebyid(int roleid) {
        String sql = "delete from role where roleid = ?";
        List list = new ArrayList();
        list.add(roleid);
        int update = update(sql, list);
        closeAll();
        return update;
    }
}
